package com.atguigu.medical.realtime.util;


import com.atguigu.medical.realtime.common.Constant;
import net.minidev.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava30.com.google.common.base.CaseFormat;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lzc
 * @Date 2023/4/21 11:21
 */
public class JdbcUtil {
    
    public static Connection getPhoenixConnection() {
        String driver = Constant.PHOENIX_DRIVER;
        String url = Constant.PHOENIX_URL;
    
        return getJdbcConnection(driver, url, null, null);
    }
    
    public  static Connection getJdbcConnection(String driver,
                                                String url,
                                                String user,
                                                String pw) {
        // 1. 加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("驱动没有找到: " + driver);
        }
        
        // 2. 通过驱动管理器获取链接对象
    
        try {
            return DriverManager.getConnection(url, user, pw);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("获取jdbc 连接异常: " + e.getMessage());
        }
    
    }
    
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    public static Connection getMysqlConnection() {
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        return getJdbcConnection(driver, url, "root", "aaaaaa");
    }
    
    // TODO
    public static <T> List<T> queryList(Connection conn, String sql, Object[] args, Class<T> tClass, boolean ... isUnderLineToCamel) {
        boolean flag = false;
        if (isUnderLineToCamel.length > 0) {
            flag = isUnderLineToCamel[0];
        }
        ArrayList<T> result = new ArrayList<>();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            // sql中可能有占位符, 给占位符赋值
            for (int i = 0; args != null && i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            // 执行查询
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
    
            while (resultSet.next()) {  // 如果是 true 表示遍历到一行数据,这时结果集会指向这行
                // 每行封装一个 T 类型的对象
                T t = tClass.newInstance();  // 创建一个 T 类型的对象
                // 把每行的多列数据封装到 T 对象的对象中的属性中
                for (int i = 1; i <= metaData.getColumnCount(); i++) {  // 遍历每一列
                    // 表的列名和 T 对象中的属性名保持一致
                    String name = metaData.getColumnLabel(i);
                    // 有可能需要做下划线到驼峰的转换
                    if (flag) {
                        // aa_bb_cc => 小驼 aaBbCc   大驼 AaBbCc
                        name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
                    }
                    Object v = resultSet.getObject(i);
                    // t.name=v
                    BeanUtils.setProperty(t, name, v);
                }
    
                result.add(t);
            }
    
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    
        return result;
    }
    
    public static void main(String[] args) {
        /*List<TableProcess> list = queryList(getMysqlConnection(),
                                            "select * from gmall2023_config.table_process where sink_table=?",
                                            new Object[]{"dim_activity_info"},
                                            TableProcess.class,
                                            true
        );*/
        /*List<JSONObject> list = queryList(getMysqlConnection(),
                                          "select * from gmall2023.user_info",
                                          new Object[]{},
                                          JSONObject.class,
                                          true
        );*/
    
        List<JSONObject> list = queryList(getPhoenixConnection(),
                                          "select * from dim_sku_info",
                                          new Object[]{},
                                          JSONObject.class,
                                          true
                                          
        );
    
        for (Object tp : list) {
            System.out.println(tp);
        }
    }
}
